﻿using System.Collections;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using AzureDiagMon.Common.Mvvm;

namespace AzureDiagMon.Common
{
    public class CheckableItemsWrapPanel : ItemsControl
    {
        public static readonly DependencyProperty CheckedItemsProperty =
            DependencyProperty.Register("CheckedItems",
                typeof(IEnumerable),
                typeof(CheckableItemsWrapPanel),
                new PropertyMetadata(default(IEnumerable), 
                    (o, args) =>
                        {
                            var sender = o as CheckableItemsWrapPanel;
                            if (sender != null)
                            {
                                var checkableCollection =
                                    sender.GetValue(CheckableCollectionProperty) as CheckableCollection;
                                if (checkableCollection != null)
                                {
                                    checkableCollection.CheckedItems = args.NewValue as IEnumerable;
                                }
                            }
                        }));

        public IEnumerable CheckedItems
        {
            get { return (IEnumerable)GetValue(CheckedItemsProperty); }
            set { SetValue(CheckedItemsProperty, value); }
        }

        public static readonly DependencyProperty CheckableCollectionProperty =
            DependencyProperty.Register("CheckableCollection",
                typeof(CheckableCollection),
                typeof(CheckableItemsWrapPanel),
                new PropertyMetadata(default(CheckableCollection)));

        public CheckableCollection CheckableCollection
        {
            get { return (CheckableCollection)GetValue(CheckableCollectionProperty); }
            set { SetValue(CheckableCollectionProperty, value); }
        }

        static CheckableItemsWrapPanel()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckableItemsWrapPanel), new FrameworkPropertyMetadata(typeof(CheckableItemsWrapPanel)));
        }

        protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
        {
            CheckableCollection = new CheckableCollection(ItemsSource);
            ((INotifyPropertyChanged)CheckableCollection).PropertyChanged
                += CheckableItemsWrapPanelPropertyChanged;
            UpdateCheckedItems();
            base.OnItemsSourceChanged(oldValue, newValue);
        }

        private void UpdateCheckedItems()
        {
            if (CheckableCollection != null && CheckableCollection.CheckedItems != null)
                CheckedItems = CheckableCollection.CheckedItems as IEnumerable;
            else
                CheckedItems = null;
        }

        void CheckableItemsWrapPanelPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CheckedItems")
            {
                UpdateCheckedItems();
            }
        }

    }
}
